package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentServiceService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import javax.swing.*;
import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentServiceService {


    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public List<TravelComment> findByDetailId(Long id) {
        if (id != null) {

            Query query = new Query();

            query.addCriteria(Criteria.where("travelId").is(id));

            List<TravelComment> travelComments = template.find(query, TravelComment.class);

            return travelComments;
        }
        return null;
    }

//    @Override
//    public Page query(TravelCommentQuery qo) {
//        Query query = new Query();
//
//        //设置查询条件
//        Criteria criteria = new Criteria();
//        criteria.where("travelId").is(qo.getTravelId());
//
//        query.addCriteria(criteria);
//
//        //查询满足条件的总数
//        long count = template.count(query, StrategyComment.class);
//
//        if (count == 0) {
//
//            //返回空
//            return Page.empty();
//        }
//
//
//        //pageable添加分页条件
//        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
//
//        //添加分页条件
//        query.with(pageable);
//
//        //查询每页数据
//        List<TravelComment> travelComments = template.find(query, TravelComment.class);
//
//        return new PageImpl(travelComments, pageable, count);
//    }

    @Override
    public TravelComment save(TravelComment travelComment) {

        travelComment.setCreateTime(new Date());

        //设置关联评论
        if (travelComment.getRefComment() != null) {

            if (StringUtils.hasLength(travelComment.getRefComment().getId())) {

                TravelComment ref_travelComment = travelCommentRepository.findById(travelComment.getRefComment().getId()).get();

                travelComment.setRefComment(ref_travelComment);
            }

        }

        travelCommentRepository.save(travelComment);

        return travelComment;
    }

    @Override
    public List<TravelComment> queryTop10OfTime() {

        Query query = new Query();

        query.with(new Sort(Sort.Direction.DESC,"createTime"));

        query.limit(10);

        List<TravelComment> travelComments = template.find(query, TravelComment.class);

        return travelComments;
    }

    @Override
    public List<TravelComment> findCommentByUid(Long id) {
        if (id != null) {

            Query query = new Query();

            query.addCriteria(Criteria.where("userId").is(id));

            List<TravelComment> travelComments = template.find(query, TravelComment.class);

            return travelComments;
        }
        return null;
    }
}
